package com.robot.service.impl;

import com.robot.model.Maintenance;
import com.robot.mapper.MaintenanceMapper;
import com.robot.service.MaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaintenanceServiceImpl implements MaintenanceService {
    
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    
    @Override
    public List<Maintenance> getMaintenances(String company, Integer year, Integer month) {
        if (month != null) {
            return maintenanceMapper.findByCompanyAndYearAndMonth(company, year, month);
        } else {
            return maintenanceMapper.findByCompanyAndYear(company, year);
        }
    }
    
    @Override
    public Maintenance saveMaintenance(Maintenance maintenance) {
        maintenanceMapper.insert(maintenance);
        return maintenance;
    }
    
    @Override
    public String getMaintenanceStatus(String company, Integer year, Integer month) {
        List<Maintenance> maintenances = maintenanceMapper.findByCompanyAndYearAndMonth(company, year, month);
        if (maintenances.isEmpty()) {
            return "无记录";
        }
        
        Maintenance maintenance = maintenances.get(0);
        return String.format("设备数量：%d，已完成维保：%d，完成率：%.2f%%", 
                maintenance.getDeviceCount(), 
                maintenance.getCompletedCount(),
                maintenance.getDeviceCount() > 0 ? 
                        (maintenance.getCompletedCount() * 100.0 / maintenance.getDeviceCount()) : 0);
    }
} 